{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d7454aba",
   "metadata": {},
   "source": [
    "* 2023-10-23\n",
    "* 课程：Python语言\n",
    "* week07"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a809a67",
   "metadata": {},
   "source": [
    "# 1. 列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cfdfff0",
   "metadata": {},
   "source": [
    "* 思考：如果把我们班同学的名字都存起来，你会怎么做？用变量name1、name2、name3....来存么？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ab8b4aa",
   "metadata": {},
   "source": [
    "## 1.1 体验列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "050e9f9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "name1 = \"张三\"\n",
    "name2 = \"李华\"\n",
    "name3 = \"李鹏\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "467595aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "805b689e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2e89da87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李鹏'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cd182231",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"张三，李华，李鹏，李小明\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d3da7323",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三，李华，李鹏，李小明'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d196e93d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e977157",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[3:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18252f7d",
   "metadata": {},
   "source": [
    "* 综上，一种新的数据结构就此而生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9e226127",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list = [\"张三\",\"李华\",\"李鹏\",\"李小明\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9882f2a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', '李鹏', '李小明']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d4b7cc91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fb07b5b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8f9c4ff8",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_学号 = [20030101001,20030101002,20030101003,20030101004]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a8b3a97c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20030101001, 20030101002, 20030101003, 20030101004]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_学号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3dfef184",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list[1] = \"李树人\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4462479f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李树人', '李鹏', '李小明']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "10019c6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_张三 = [\"张三\",20030101001,19,180,75,\"Jack\",[\"Python\",\"移动互联网\",\"UI设计\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4c48426a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 20030101001, 19, 180, 75, 'Jack', ['Python', '移动互联网', 'UI设计']]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_张三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "265dd5ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 允许重复的数据**(数据重复性)**\n",
    "网三_Python成绩 = [95,97,94,94,95,97]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "50da84a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[95, 97, 94, 94, 95, 97]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_Python成绩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82d135d9",
   "metadata": {},
   "source": [
    "## 1.2 创建列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "986637e6",
   "metadata": {},
   "source": [
    "> 1. 使用list()创建列表\n",
    "> 2. 直接使用[]创建列表,中间的元素用英文的 , 隔开\n",
    "> 3. 创建一个空列表，方法也是上面两种"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6cc73503",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18, 19, 18, 20]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 1. 使用list()创建列表\n",
    "my_list1 = list((18,19,18,20))\n",
    "print(my_list1)\n",
    "print(type(my_list1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1c68dee8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[75, 80, 74, 75]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 2. 直接使用[]创建列表,中间的元素用英文的 , 隔开\n",
    "my_list2 = [75,80,74,75]\n",
    "print(my_list2)\n",
    "print(type(my_list2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4f43b9d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['p', 'y', 't', 'h', 'o', 'n']\n"
     ]
    }
   ],
   "source": [
    "my_list3 = list(\"python\")\n",
    "print(my_list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e8314bfe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['H', 'e', 'l', 'l', 'o', ' ', 'L', 'i', 's', 't', '~']\n"
     ]
    }
   ],
   "source": [
    "my_list4 = list(\"Hello List~\")\n",
    "print(my_list4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c2b3cef5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 3. 创建空列表\n",
    "empty_list = list()\n",
    "print(empty_list)\n",
    "print(type(empty_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41d663d9",
   "metadata": {},
   "source": [
    "## 1.3 查看列表的长度 ： len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "983048a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(my_list4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4918eba",
   "metadata": {},
   "source": [
    "## 1.4 列表的取值和切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a10d00d",
   "metadata": {},
   "source": [
    "* index：取值\n",
    "* slice：切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6eb55855",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['p', 'y', 't', 'h', 'o', 'n']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list = list(\"python\")\n",
    "python_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "fd786d24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3456b10c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'n'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4de3d309",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[4:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c06575ca",
   "metadata": {},
   "source": [
    "## 1.5 列表的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55c3edce",
   "metadata": {},
   "source": [
    "* 笔记：数据如何处理是数据诞生之后非常重要的事情\n",
    "\n",
    "* 数据的基本操作流程："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77ea3bd3",
   "metadata": {},
   "source": [
    "> 1. 增加 add\n",
    "> 2. 减少/删除 remove\n",
    "> 3. 查询 find\n",
    "> 4. 修改 update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d8120df6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', '李鹏', '李小明']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李华\",\"李鹏\",\"李小明\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff2489f3",
   "metadata": {},
   "source": [
    "## 1. 增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "56a0e0c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "help(name_list.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "31cf02d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"李树人\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "1295cb92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', '李鹏', '李小明', '李树人']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "cd180cbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d32f2340",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.insert(2,\"Marry\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "f48fa465",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', 'Marry', '李鹏', '李小明', '李树人']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bee80bec",
   "metadata": {},
   "source": [
    "## 2.删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "26f8eca8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b1ef4376",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李树人'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "40df310f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', 'Marry', '李鹏', '李小明']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "6cd5e633",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "6191ff81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 'Marry', '李鹏', '李小明']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "54330c95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1f6a787f",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.remove('李鹏')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "04881743",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 'Marry', '李小明']"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ba1debe",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
